package com.hyt.it.ogt.kq.service.gov.schedule.impl.support;

import java.util.Map;
import java.util.UUID;

import com.hyt.it.ogt.kq.service.gov.schedule.model.Candidate;
import com.hyt.it.ogt.kq.service.gov.schedule.strategy.ScheduleHelper;
import com.hyt.it.ogt.kq.service.gov.schedule.strategy.ScheduleStrategy;
import com.hyt.it.ogt.kq.service.gov.schedule.utils.BitSetUtils;

import java.util.BitSet;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hyt.it.ogt.kq.service.gov.schedule.impl.ExamRoomScheduleImpl.PackageExamRoomCandidate;

/***
 * 将多批次考生组合成虚拟考生
 */
public interface CandidateTogether {
    static final Logger logger = LoggerFactory.getLogger(CandidateTogether.class);

    public void togetherCandidate(Map<String, Map<String, PackageExamRoomCandidate>> packageExamRoomCandidate);

    /**
     * 批定批次号创建虚拟用户ID
     */
    default void createCandidateVirId(List<String> batchNOList, List<Candidate> candidates) {
        String candidateVirID = UUID.randomUUID().toString();
        ScheduleHelper scheduleHelper = ScheduleHelper.getInstance();
        for (int index = 0; index < candidates.size(); index++) {
            Candidate candidate = candidates.get(index);
            String batchNO = batchNOList.get(index);
            if (!scheduleHelper.getVirId2CandidateIdMap().containsKey(candidateVirID)) {
                scheduleHelper.getVirId2CandidateIdMap().put(candidateVirID, new HashMap<>());
            }
            scheduleHelper.getVirId2CandidateIdMap().get(candidateVirID).put(batchNO,
                    candidate.getBmStudentId());
        }
    }

    /**
     * 创建考生的虚拟用户ID， 并保存到全局缓存
     */
    default void createCandidateVirId(String batchNO, Candidate candidate) {
        String candidateVirID = UUID.randomUUID().toString();
        ScheduleHelper scheduleHelper = ScheduleHelper.getInstance();
        scheduleHelper.getVirId2CandidateIdMap().put(candidateVirID, new HashMap<>());
        scheduleHelper.getVirId2CandidateIdMap().get(candidateVirID).put(batchNO,
                candidate.getBmStudentId());
    }

    /**
     * 根据用户特性码来创建虚拟考生
     * 
     * @param candidates     可组合的考生
     * @param featureCodeMap 考生的批次特征码Map
     */
    default void createCandidateVirId(List<Candidate> candidates, Map<String, BitSet> featureCodeMap,
            List<String> batchNOList) {
        String candidateVirID = UUID.randomUUID().toString();
        ScheduleHelper scheduleHelper = ScheduleHelper.getInstance();
        Collections.sort(batchNOList);
        for (Candidate candidate : candidates) {
            // 该考生的批次特征码
            BitSet batchFeaturCode = featureCodeMap.get(candidate.getBmStudentId());
            // 根据特征码得到考生在哪些批次号存在
            int[] candiateBatchIndexs = BitSetUtils.convertBitSet2Index(batchFeaturCode);
            for (int batchIndex = 0; batchIndex < candiateBatchIndexs.length; batchIndex++) {
                String batchNO = batchNOList.get(candiateBatchIndexs[batchIndex]);
                if (!scheduleHelper.getVirId2CandidateIdMap().containsKey(candidateVirID)) {
                    scheduleHelper.getVirId2CandidateIdMap().put(candidateVirID, new HashMap<>());
                }
                scheduleHelper.getVirId2CandidateIdMap().get(candidateVirID).put(batchNO,
                        candidate.getBmStudentId());
            }
        }
    }

}
